package com.wanxin.wb.common.support.util;

/**
 * feiniu.com Inc.
 * Copyright (c) 2013-2014 All Rights Reserved.
 */


import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <B>Description:</B> 扩展org.springframework.beans.BeanUtils  <br>
 * <B>Create on:</B> 2014年12月16日 下午5:47:17<br>
 *
 * @author yuan.qin
 * @version 1.0
 */
public class BeanUtils extends org.springframework.beans.BeanUtils {

    /**
     * 覆写了org.springframework.beans.BeanUtils中的同名方法
     * 源对象中字段是空的不复制
     * @param source 源
     * @param target 目标
     * @throws BeansException
     */
    public static void copyPropertiesNotNull(Object source, Object target) throws BeansException {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");
        Class<?> actualEditable = target.getClass();
        PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
        for (PropertyDescriptor targetPd : targetPds) {
            if (targetPd.getWriteMethod() != null) {
                PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(),
                    targetPd.getName());
                if (sourcePd != null && sourcePd.getReadMethod() != null) {
                    try {
                        Method readMethod = sourcePd.getReadMethod();
                        if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                            readMethod.setAccessible(true);
                        }
                        Object value = readMethod.invoke(source);

                        if (value != null) {
                            Method writeMethod = targetPd.getWriteMethod();
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }
                            writeMethod.invoke(target, value);
                        }
                    } catch (Throwable ex) {
                        throw new FatalBeanException(
                            "Could not copy properties from source to target", ex);
                    }
                }
            }
        }
    }

    /**
     * 
     * <B>Description:</B> 对象分解为Map <br>
     * <B>Create on:</B> 2015年4月1日 上午9:18:10<br>
     *
     * @param bean
     * @return
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException 
     * @author 
     */
    public static Map<String, Object> describe(Object bean) throws IllegalAccessException,
                                                           InvocationTargetException,
                                                           NoSuchMethodException {

        if (bean == null) {
            return (new java.util.HashMap<String, Object>());
        }
        if (bean instanceof Map) {
            Map map = new HashMap<String, Object>();
            map.putAll((Map) bean);
            return map;
        }

        Map<String, Object> description = new HashMap<String, Object>();
        Class<?> actualEditable = bean.getClass();
        PropertyDescriptor[] descriptors = getPropertyDescriptors(actualEditable);
        for (PropertyDescriptor targetPd : descriptors) {
            if (targetPd.getReadMethod() != null) {
                try {
                    Method readMethod = targetPd.getReadMethod();
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }
                    Object value = readMethod.invoke(bean);
                    description.put(targetPd.getName(), value);
                } catch (Throwable ex) {
                    throw new FatalBeanException("Could not copy properties from source to target",
                        ex);
                }
            }
        }
        return (description);

    }

    /**
     * 覆写了List方法
     *
     * @param source 源
     * @param destinationClass 目标
     * @throws BeansException
     */
    public static <E> List<E> copyPropertiesList(List<?> source, Class<E> destinationClass)
                                                                                           throws BeansException {
        Assert.notNull(source, "Source must not be null");

        List<E> res = new ArrayList<E>(source.size());
        for (Object o : source) {
            E e;
            try {
                e = destinationClass.newInstance();
//                BeanUtils.copyProperties(o, e);
                BeanUtils.copyPropertiesNotNull(o, e);//Add by xiangyu.ye 2016/4/6 15:01
                res.add(e);
            } catch (InstantiationException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
        return res;

    }

}
